/**
 * 
 */
package com.android.dwt.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.android.dwt.R;
import com.android.dwt.R.id;
import com.android.dwt.R.layout;
import com.android.dwt.R.string;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

/**
 * @author xiang
 *
 */
public class RemindAct extends Activity implements OnDateChangedListener, OnClickListener,
	OnTimeChangedListener {
	
	public static final int RESULT_REMIND = 5;
	
	private TextView mSelectedDate;
	private TextView mSelectedTime;
	private DatePicker dp_remind;
	private TimePicker tp_remind;
	protected String TAG = "RemindAct";
	
	private Calendar calRmd;		// remind time
	private Calendar mCal;			// current set time
	
	private SimpleDateFormat mSdfDate = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat mSdfTime = new SimpleDateFormat("HH:mm");
	private SimpleDateFormat mSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.remind);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_cmn);
		
		((TextView) findViewById(R.id.tv_title)).setText(getString(R.string.title_remind));
		((ImageButton) findViewById(R.id.btn_new)).setVisibility(View.INVISIBLE);
		
		calRmd = Calendar.getInstance();		// set to be current time
		mCal = Calendar.getInstance();
		
		mSelectedDate = (TextView) findViewById(R.id.tv_select_date);
		mSelectedTime = (TextView) findViewById(R.id.tv_select_time);
		
		dp_remind = (DatePicker) findViewById(R.id.dp_remind);
		tp_remind = (TimePicker) findViewById(R.id.tp_remind);
		tp_remind.setIs24HourView(true);
		
		if (getIntent().hasExtra("time")) {			// remind has been set previously
			
			calRmd = (Calendar) getIntent().getSerializableExtra("time");
		}
		
		/*if (preTime.length() > 0) {  // remind time has been set
			
			try {
				Date datePreTime = mSdf.parse(preTime);
				calPre.setTime(datePreTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		
		dp_remind.init(calRmd.get(Calendar.YEAR),
				calRmd.get(Calendar.MONTH),
				calRmd.get(Calendar.DAY_OF_MONTH),
				this);
		
		/*int year = dp_remind.getYear();
		int month = dp_remind.getMonth()+1;
		int day = dp_remind.getDayOfMonth();
		int hour = tp_remind.getCurrentHour();
		int minute = tp_remind.getCurrentMinute();*/
		
		mSelectedDate.setText(mSdfDate.format(calRmd.getTime()));
		mSelectedTime.setText(mSdfTime.format(calRmd.getTime()));
		
		tp_remind.setCurrentHour(calRmd.get(Calendar.HOUR_OF_DAY));
		tp_remind.setCurrentMinute(calRmd.get(Calendar.MINUTE));
		
		/*calRmd.set(calPre.get(Calendar.YEAR),
				calPre.get(Calendar.MONTH),
				calPre.get(Calendar.DAY_OF_MONTH),
				calPre.get(Calendar.HOUR_OF_DAY),
				calPre.get(Calendar.MINUTE));*/
		
		Log.e(TAG, "calRmd: "+calRmd.get(Calendar.YEAR)+"-"+calRmd.get(Calendar.MONTH)
				+"-"+calRmd.get(Calendar.DAY_OF_MONTH)
				+" "+calRmd.get(Calendar.HOUR_OF_DAY)
				+":"+calRmd.get(Calendar.MINUTE));
		
		handleEvent();
	}
	
	/*private String format2(int x) {
		String s = "" + x;
		if (s.length() == 1)
			s = "0" + s;
		
		return s;
	}*/

	private void handleEvent() {
		// TODO Auto-generated method stub
		tp_remind.setOnTimeChangedListener(this);
		
		((Button) findViewById(R.id.btn_remind_ok)).setOnClickListener(this);
		
		((Button) findViewById(R.id.btn_remind_cal)).setOnClickListener(this);
		
//		((ImageButton) findViewById(R.id.btn_return)).setOnClickListener(this);
		ImageButton ibReturn = (ImageButton) findViewById(R.id.btn_return);
		ibReturn.setOnClickListener(this);
		ibReturn.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					
					v.setBackgroundResource(R.drawable.back_pressed);
					
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					
					v.setBackgroundResource(R.drawable.back);
				}

				return false;
			}
			
		});
		
	}

	private void saveRemindTime() {
		
		Calendar cldCur = Calendar.getInstance();		// get current time
		
		cldCur.clear(Calendar.MILLISECOND);
		mCal.clear(Calendar.MILLISECOND);
		
		cldCur.set(Calendar.SECOND, 0);
		mCal.set(Calendar.SECOND, 0);
		
		Log.e(TAG, "cldCur: "+mSdf.format(cldCur.getTime()));
		Log.e(TAG, "mCal: "+mSdf.format(mCal.getTime()));
		Log.e(TAG, "cldCur millis: 	"+cldCur.getTimeInMillis());
		Log.e(TAG, "mCal millis: 	"+mCal.getTimeInMillis());
		Log.e(TAG, "compare result: " + cldCur.compareTo(mCal));
		
		if (cldCur.after(mCal)) {
			/*Toast.makeText(RemindAct.this,
					"remind time: "+calRmd.get(Calendar.YEAR)+"-"+calRmd.get(Calendar.MONTH)
					+"-"+calRmd.get(Calendar.DAY_OF_MONTH)+" "+calRmd.get(Calendar.HOUR_OF_DAY)
					+":"+calRmd.get(Calendar.MINUTE),
					Toast.LENGTH_LONG).show();*/
			
			Toast.makeText(RemindAct.this, getString(R.string.msg_rmd_cur), Toast.LENGTH_LONG)
				.show();
			
			// restore the previous time
			mSelectedDate.setText(mSdfDate.format(calRmd.getTime()));
			mSelectedTime.setText(mSdfTime.format(calRmd.getTime()));
			
			
			dp_remind.updateDate(calRmd.get(Calendar.YEAR),
					calRmd.get(Calendar.MONTH),
					calRmd.get(Calendar.DAY_OF_MONTH));		// it won't trigger dateChanged event
			
			// it would trigger timeChanged event
			tp_remind.setCurrentHour(calRmd.get(Calendar.HOUR_OF_DAY));
			tp_remind.setCurrentMinute(calRmd.get(Calendar.MINUTE));
			
			// set $mCal to $calRmd
			mCal.setTime(calRmd.getTime());
			
		} else {
			
			calRmd.setTime(mCal.getTime());
			
			Intent itt = new Intent();
//			Bundle bd = new Bundle();
			
//			bd.putString("time", strTime);
//			itt.putExtras(bd);
			
			itt.putExtra("time", calRmd);
			
			setResult(RESULT_REMIND, itt);
			
			finish();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		
			case R.id.btn_remind_ok:
				
				saveRemindTime();
				
				break;
				
			case R.id.btn_return:
			case R.id.btn_remind_cal:
				finish();
				
				break;
			
			default:
				break;
		}
	}
	
	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		// TODO Auto-generated method stub
		
		mCal.set(Calendar.YEAR, year);
		mCal.set(Calendar.MONTH, monthOfYear);
		mCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
		
		Log.e(TAG, "date changed:");
		
		mSelectedDate.setText(mSdfDate.format(mCal.getTime()));
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		// TODO Auto-generated method stub
//		Calendar cld = (Calendar) calRmd.clone();
		
		mCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
		mCal.set(Calendar.MINUTE, minute);
		
		Log.e(TAG, "time changed:");
		
		mSelectedTime.setText(mSdfTime.format(mCal.getTime()));
	}

}	// end RemindAct
